package servlet;

import domain.User;
import org.apache.commons.beanutils.BeanUtils;
import service.UserService;
import service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/addUserServlet")
public class AddUserServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        Map<String, String[]> map = request.getParameterMap( );
        User user = new User() ;
        try {
            BeanUtils.populate(user,map);
            System.out.println(user );
        } catch (IllegalAccessException e) {
            e.printStackTrace( );
        } catch (InvocationTargetException e) {
            e.printStackTrace( );
        }
        UserService service = new UserServiceImpl() ;
        service.addUser(user);


        //3. 重定向查询servlet
        response.sendRedirect("/day20/findPageUserServlet");


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.doPost(request,response);
    }
}
